Imports SubSonic
Imports ClubStarterKit.Data
Imports ClubStarterKit.Web
Imports System.Linq

Partial Class Statistics_Game
    Inherits System.Web.UI.Page
    Dim MatchId As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request("id") Is Nothing Then
            Response.Redirect("~/CricketStats/default.aspx")
        End If

        MatchId = CInt(Request("id"))
        Dim selectedMatch As CricketBLL.Match = New CricketBLL.Match()
        Dim sb As StringBuilder = New StringBuilder()

        If IsPostBack = False Then
            Try
                Using db As New CricketBLL.CricketStatsDataContext(Settings.DatabaseConnectionString())

                    Dim matches = From m In db.Matches _
                                  Where m.MatchId = MatchId _
                                  Select m

                    If matches Is Nothing = False Then
                        selectedMatch = matches.First()
                    End If
                    'Game Title
                    If selectedMatch.IsHome Then
                        sb.Append(selectedMatch.Team.TeamName)
                        sb.Append(" vs ")
                        sb.Append("<i>" & selectedMatch.OpponentTeam & "</i>")
                        GameTitle.Text = sb.ToString()
                    Else
                        sb.Append("<i>" & selectedMatch.OpponentTeam & "</i>")
                        sb.Append(" vs ")
                        sb.Append(selectedMatch.Team.TeamName)
                        GameTitle.Text = sb.ToString()
                    End If

                    lblTeamName.Text = selectedMatch.Team.TeamName
                    lblTeamOvers.Text = selectedMatch.TeamOvers
                    lblTeamRuns.Text = selectedMatch.TeamRuns
                    lblTeamWickets.Text = selectedMatch.TeamWickets
                    lblOppName.Text = selectedMatch.OpponentTeam
                    lblOppOvers.Text = selectedMatch.OppOvers
                    lblOppRuns.Text = selectedMatch.OppRuns
                    lblOppWickets.Text = selectedMatch.OppWickets

                    'Bind stats
                    BindStats()
                End Using

            Catch ex As Exception
                GameTitle.Text = "<h3 style='width: 100%; text-align: center;'>There is no game data</h3>"
                divScore.Visible = False
            End Try

        End If


        IsGameRecorder = User.IsInRole("GameRecorder")

        If IsGameRecorder = True Then
            'EditScore.NavigateUrl = "~/CricketStats/EditScore.aspx?gameid=" & Request("ID").ToString()
            EditScore.NavigateUrl = "javascript:OpenModal('" + "../CricketStats/EditScore.aspx?gameid=" & Request("ID").ToString() + "')"
            hlEditBatting.NavigateUrl = "~/CricketStats/EditStat.aspx?gameid=" & Request("ID").ToString() & "#batting"
            hlEditBowling.NavigateUrl = "~/CricketStats/EditStat.aspx?gameid=" & Request("ID").ToString() & "#bowling"
        End If
    End Sub

    Private Sub BindStats()
        Using dbc As New CricketBLL.CricketStatsDataContext(Settings.DatabaseConnectionString)

            Dim batting = From b In dbc.PlayerStats _
                          Where b.MatchId = MatchId _
                          Order By b.BatNumber Ascending _
                          Select b
            gvBatting.DataSource = batting
            gvBatting.DataBind()

            gvBowling.DataSource = From b In batting _
                                   Where b.OversBowled.HasValue _
                                   Order By b.BowlerNumber Ascending _
                                   Select b
            gvBowling.DataBind()

        End Using
    End Sub

    Private m_isrec As Boolean = False
    Public Property IsGameRecorder() As Boolean
        Get
            Return m_isrec
        End Get
        Set(ByVal value As Boolean)
            m_isrec = value
        End Set
    End Property


End Class
